package com.jstyle.jclifexd.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.jstyle.jclifexd.R;
import com.jstyle.jclifexd.model.Activitities;
import com.jstyle.jclifexd.model.Search;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class SearchAdapter extends  RecyclerView.Adapter<BaseViewHolder>{
    List<Search> Details;
    Context context;
    //私有属性
    private OnItemClickListener onItemClickListener = null;

    public interface OnItemClickListener {
        void onItemClick(Search aa, int position);
    }

    //setter方法
    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public SearchAdapter(Context mycontext) {
        this.context = mycontext;
        this.Details = new ArrayList<>();
    }

    public void SetData(List<Search> myDetails) {
        this. Details = myDetails;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public BaseViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new ViewHolder( LayoutInflater.from(context).inflate(R.layout.item_search, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(final BaseViewHolder holder, final int position) {
        final Search sleepDetail = Details.get(position);
        final ViewHolder viewHolder = (ViewHolder) holder;
         viewHolder.locatin_name.setText(sleepDetail.getName());
         viewHolder.locatin_info.setText(sleepDetail.getInfo());
         if(sleepDetail.isCheck()){
             viewHolder.imageview.setVisibility(View.VISIBLE);
         }else{
             viewHolder.imageview.setVisibility(View.GONE);
         }

        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (onItemClickListener != null) {
                    for (int i=0;i<Details.size();i++){
                        if(i==position){
                            Details.get(i).setCheck(true);
                        }else{
                            Details.get(i).setCheck(false);
                        }
                    }
                    notifyDataSetChanged();
                    onItemClickListener.onItemClick(sleepDetail, position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return null==Details?0:Details.size();
    }



    static class ViewHolder extends BaseViewHolder {
        @BindView(R.id.locatin_name)
        TextView locatin_name;
        @BindView(R.id.locatin_info)
        TextView locatin_info;
        @BindView(R.id.checkok)
        ImageView imageview;
        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

}
